<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
// import { color } from 'echarts/lib/export'
require('echarts/theme/macarons') // echarts theme
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      // this.setOptions(this.chartData)
      this.setOptions()
    },
    // setOptions() {
    setOptions({ bus_type_name, total_num } = {}) {
      this.chart.setOption({
        title: {
          text: '笔数统计',
          textStyle: {
            color: '#333',
            fontSize: 16,
            fontStyle: 'normal'
          }
          // top: '5%'

        },

        legend: {
          data: ['HIS笔数'],
          textStyle: {
            color: '#333'
          },
          right: '10%'
          // top: '%'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          left: '2%',
          right: '2%',
          bottom: '3%',
          top: '17%',
          containLabel: true,
          show: false,
          backgroundColor: 'transparent',
          borderColor: 0
        },
        xAxis: [
          {
            type: 'category',
            data: bus_type_name,
            // data: ['平台(自助机)笔数', 'HIS系统笔数', '银行笔数', '异常笔数'],
            axisLine: {
              show: true,
              lineStyle: {
                color: '#E1E1E1',
                width: 1,
                type: 'solid'
              }
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: '#566588'
              }
            }
          }

        ],
        yAxis: [
          {
            type: 'value',
            name: '(笔)',
            nameTextStyle: {
              color: '#4A5A7F',
              fontSize: 14,
              padding: [0, 0, 0, -35]
            },
            splitLine: {
              show: true,
              lineStyle: {
                type: 'dashed',
                color: '#E1E1E1'
              }
            },
            axisLine: { // 轴线
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              textStyle: {
                color: '#566588' // 坐标的字体颜色
              }
            }
          }
        ],
        series: [
          {
            name: '笔数',
            type: 'bar',
            barWidth: '24',
            itemStyle: {
              // 通常情况下：
              normal: {
                barBorderRadius: [5, 5, 0, 0],
                // 每个柱子的颜色即为colorList数组里的每一项，如果柱子数目多于colorList的长度，则柱子颜色循环使用该数组
                color: function(params) {
                  var colorList = [
                    ['#00C6FF', '#0072FF'],
                    ['#6BE9CF', '#58C9F4'],
                    ['#FFE259', '#FFA751'],
                    ['#FF9966', '#FF5E62']
                  ]
                  var index = params.dataIndex
                  if (params.dataIndex >= colorList.length) {
                    index = params.dataIndex - colorList.length
                  }
                  return new echarts.graphic.LinearGradient(0, 0, 0, 1,
                    [{
                      offset: 0,
                      color: colorList[index][0]
                    },
                    {
                      offset: 1,
                      color: colorList[index][1]
                    }
                    ])
                }
              }
            },
            data: total_num
          }

        ]
      })
    }
  }
}
</script>
